package tests.cases {
	
	import mx.rpc.AsyncToken;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import org.flexunit.Assert;
	
	import tests.cases.DefaultTestCase;
	
	public class TestGroupDirectory extends DefaultTestCase {
		
		private var groupBrowserService:RemoteObject;
		
		[Before]
		public function setUp():void {
			
			groupBrowserService = new RemoteObject();
			groupBrowserService.destination = "amfphp";
			groupBrowserService.source = "GroupBrowserService";
		}
		
		[After]
		public function tearDown():void {
			
			groupBrowserService = null;
		}
		
		// ----------------------------------------------------------------
		// TEST GET GROUP LIST
		// ----------------------------------------------------------------
		[Test(async,order=1)]
		public function testGetGroupList():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				userUID: "user001"
			};
			
			var token:AsyncToken = groupBrowserService.getGroupList(argsVO.sessionID, argsVO.userUID);
			runAsyncTest(token, argsVO, handleGetGroupList);
		}
		
		private function handleGetGroupList(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			Assert.assertTrue((message.result.length > 0));
		}
		
		// ----------------------------------------------------------------
		// TEST GET GROUP DETAILS
		// ----------------------------------------------------------------
		[Test(async,order=2)]
		public function testGetGroupDetails():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				userUID: "user001"
			};
			
			var token:AsyncToken = groupBrowserService.getGroupDetails(argsVO.sessionID, argsVO.userUID);
			runAsyncTest(token, argsVO, handleGetGroupDetails);
		}
		
		private function handleGetGroupDetails(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			
			Assert.assertNotNull(message.result.noMembers);
			Assert.assertNotNull(message.result.admins);
			Assert.assertNotNull(message.result.sponsors);
		}
		
		// ----------------------------------------------------------------
		// TEST LEAVE GROUP
		// ----------------------------------------------------------------
		[Test(async,order=3)]
		public function testLeaveGroup():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				userUID: "user001",
				groupUID: "group001"
			};
			
			var token:AsyncToken = groupBrowserService.leaveGroup(argsVO.sessionID, argsVO.userUID, argsVO.groupUID);
			runAsyncTest(token, argsVO, handleLeaveGroup);
		}
		
		private function handleLeaveGroup(message:ResultEvent, passedData:Object):void {
			
			Assert.assertEquals("group001", message.result);
			
		}
		
		// ----------------------------------------------------------------
		// TEST JOIN GROUP
		// ----------------------------------------------------------------
		[Test(async,order=4)]
		public function testJoinGroup():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				userUID: "user001",
				groupUID: "group001"
			};
			
			var token:AsyncToken = groupBrowserService.joinGroup(argsVO.sessionID, argsVO.userUID, argsVO.groupUID);
			runAsyncTest(token, argsVO, handleJoinGroup);
		}
		
		private function handleJoinGroup(message:ResultEvent, passedData:Object):void {
			
			Assert.assertEquals("group001", message.result);
		}
	}
}